﻿define(['plugins/dialog', 
        'toastr',
        'i18n',
        'common/dateUtils'], 
	function (dialog, toastr, i18n, DateUtils) {
	
    var viewDetail = function () {
    	this.title = "Popup Demo 1";
    	this.okButtonText = "OK";
    	
    	this.typeOptions = [
      	    {key: 'T1', label: 'Type 1', keyStr: 'T1'},
      	    {key: 'T2', label: 'Type 2', keyStr: 'T2'},
      	    {key: 'T3', label: 'Type 3', keyStr: 'T3'}
      	];
    	
    	this.tiers = [
    	    {adresse: { codePostal: '0000', ville: 'ville 0', rue: 'rue 0', numero: 'numero 0'}, type: {key: 'T1'}, du: DateUtils.getDate('2014-01-01'), au: DateUtils.getDate('2014-01-05')},
    	    {adresse: { codePostal: '1111', ville: 'ville 1', rue: 'rue 1', numero: 'numero 1'}, type: {key: 'T1'}, du: DateUtils.getDate('2014-01-06'), au: DateUtils.getDate('2014-01-10')},
    	    {adresse: { codePostal: '2222', ville: 'ville 2', rue: 'rue 2', numero: 'numero 2'}, type: {key: 'T1'}, du: DateUtils.getDate('2014-01-11'), au: DateUtils.getDate('2014-01-15')},
    	    {adresse: { codePostal: '3333', ville: 'ville 3', rue: 'rue 3', numero: 'numero 3'}, type: {key: 'T1'}, du: DateUtils.getDate('2014-01-16'), au: DateUtils},
    	    {adresse: { codePostal: '4444', ville: 'ville 4', rue: 'rue 4', numero: 'numero 4'}, type: {key: 'T2'}, du: DateUtils.getDate('2014-01-01'), au: DateUtils.getDate('2014-01-03')},
    	    {adresse: { codePostal: '6666', ville: 'ville 6', rue: 'rue 6', numero: 'numero 6'}, type: {key: 'T2'}, du: DateUtils.getDate('2014-01-07'), au: DateUtils.getDate('2014-01-09')},
    	    {adresse: { codePostal: '7777', ville: 'ville 7', rue: 'rue 7', numero: 'numero 7'}, type: {key: 'T2'}, du: DateUtils.getDate('2014-01-10'), au: DateUtils.getDate('2014-01-12')},
    	    {adresse: { codePostal: '5555', ville: 'ville 5', rue: 'rue 5', numero: 'numero 5'}, type: {key: 'T2'}, du: DateUtils.getDate('2014-01-04'), au: DateUtils.getDate('2014-01-06')},
    	    {adresse: { codePostal: '8888', ville: 'ville 8', rue: 'rue 8', numero: 'numero 8'}, type: {key: 'T3'}, du: DateUtils.getDate('2014-05-01'), au: DateUtils.getDate('2014-05-31')},
    	    {adresse: { codePostal: '9999', ville: 'ville 9', rue: 'rue 9', numero: 'numero 9'}, type: {key: 'T3'}, du: DateUtils.getDate('2014-06-01'), au: DateUtils.getDate('2014-06-08')}
    	];
    	
    	this.tier = {
    		adresse: {
    			codePostal: ko.observable(null),
        	    ville: ko.observable(null), 	
        	    rue: ko.observable(null),
        	    numero: ko.observable(null)	
    		},
    		type: {
    			key: ko.observable(null)
    		},
    		du: ko.observable(null),
    		au: ko.observable(null)
		};
    	
    	this.i18n = i18n;
    	this.formErrors = ko.observableArray();
    	this.msgOptions = ko.observableArray();
    	this.msgOptionsTitle = ko.observable();
    	this.msgOptionValue = ko.observable();
    	this.msgSelectChanged = msgSelectChanged;
    	this.typeChanged = typeChanged;
    };

    viewDetail.prototype.close = close;
    viewDetail.prototype.submit = submit;
    
    return viewDetail;
    
    function close() {
        dialog.close(this);
    }

    function submit() {
    	toastr.info('Result: ' + moment(getLargestHistoryAu(this.tiers, this.tier.type.key())).format('DD.MM.YYYY'));
    }
    
    function msgSelectChanged() {
    	$('#'+this.element).focus();
    }
    
    function typeChanged() {
    	this.formErrors.push({ message: isTypeExistActiveAdresse(this.tiers, this.tier.type.key()) });
    }
    
    function isTypeExistActiveAdresse(tiers, type) {
    	var result = _.find(tiers, function(tier){
    		return tier.type.key === type.key && (_.isEmpty(tier.au) || DateUtils.dateGe(tier.au, DateUtils.currentDate())); 
    	});
    	
    	if (result.length > 0) {
    		return true;
    	}
    	return false;
    }
    
    function getLargestHistoryAu(tiers, type) {
    	return _.reduce(tiers, function(largestAu, tier){
    		return (tier.type.key === type)
    			&& (tier.au && DateUtils.dateLt(tier.au, DateUtils.currentDate())) 
    			&& DateUtils.dateGt(tier.au, largestAu) ? tier.au : largestAu;  
    	}, 0);
    }
});